home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vuser / pty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  2.2 KB  |  100 lines

  1. /* pty.c - This module contains the code to handle the pty stuff.
  2.  
  3.     Copyright 1989 by Jeffrey F. Lawhorn  (jeffl@berick.uucp)
  4.  
  5.     This file is part of vuser.
  6.  
  7.     vuser is free software; you can redistribute it and/or modify it
  8.     under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your
  10.     option) any later version.
  11.  
  12.     vuser is distributed in the hope that it will be useful, but
  13.     WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.     General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GNU CC; see the file COPYING.  If not, write to the
  19.     Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #if !defined(lint)
  23. static char SCCSid[] = "$Id: pty.c,v 1.1 89/12/15 21:31:03 jeffl Exp $";
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28.  
  29. #if defined(__STDC__)
  30. extern void Tty2ShellMode();
  31. #else
  32. extern void Tty2ShellMode();
  33. #endif
  34.  
  35. #if defined(SYSV)
  36. #include <termio.h>
  37. struct termio ShellTtyMode;
  38. #else
  39. #include <sgtty.h>
  40. struct sgttyb ShellTtyMode;
  41. #endif
  42.  
  43. int MasterPty = -1, SlavePty = -1;
  44.  
  45. void Pty2ShellMode()
  46. {
  47. #if defined(SYSV)
  48.     ioctl(SlavePty, TCSETA, &ShellTtyMode);
  49. #else
  50.     ioctl(SlavePty, TIOCSETP, &ShellTtyMode);
  51. #endif
  52.     return;
  53. }
  54.  
  55. void GetPty()
  56. {
  57.     int line = 'p', node = 0;
  58.     char tmp[32];
  59.  
  60.     strcpy(tmp, "/dev/ptyxx");
  61.     for(; line <= 'q'; ++line)
  62.     for(node = 0; node < 16; ++node) {
  63.         tmp[8] = (char)line;
  64.         tmp[9] = (char)((node > 9 ? (node + 'a' - 10) : (node + '0')));
  65.         if((MasterPty = open(tmp, O_RDWR|O_NDELAY)) >= 0) {
  66.         tmp[5] = 't';
  67.         if((SlavePty = open(tmp, O_RDWR)) < 0) {
  68.             perror("on pty: ");
  69.             Tty2ShellMode();
  70.             exit(1);
  71.         }
  72.         Pty2ShellMode();
  73.         return;
  74.         }
  75.     }
  76.     fprintf(stderr, "No ptys available, try later.\n");
  77.     Tty2ShellMode();
  78.     exit(1);
  79. }
  80.  
  81. void SlavePty2Stdio()
  82. {
  83.     int cnt = 3;
  84. #if !defined(_NFILE)
  85.     int maxFiles = getdtablesize();
  86. #else
  87.     int maxFiles = _NFILE;
  88. #endif
  89.     
  90.     close(0);
  91.     dup(SlavePty);
  92.     close(1);
  93.     dup(SlavePty);
  94.     close(2);
  95.     dup(SlavePty);
  96.     for(; cnt < maxFiles; ++cnt)
  97.     close(cnt);
  98.     return;
  99. }
  100.